package cn.edu.fzu.delivery.service.impl;

import cn.edu.fzu.delivery.domain.entity.Courier;
import cn.edu.fzu.delivery.domain.entity.CourierDelivery;
import cn.edu.fzu.delivery.domain.entity.Logistics;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.CourierStatusEnum;
import cn.edu.fzu.delivery.domain.enums.DeliveryStatusEnum;
import cn.edu.fzu.delivery.exception.ServiceException;
import cn.edu.fzu.delivery.service.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author jiebin
 * @data 2022/11/9
 **/
@Service
public class DeliveryManServiceImpl implements DeliveryManService {

    @Resource
    private CourierService         courierService;
    @Resource
    private LogisticsService       logisticsService;
    @Resource
    private CourierDeliveryService courierDeliveryService;
    @Resource
    private NetworkAdminService    networkAdminService;

    @Override
    public List<CourierDelivery> listCouriers(Long deliveryManId, String courierId, DeliveryStatusEnum status,
                                              String startTime, String endTime) {
        return courierDeliveryService.list(deliveryManId, courierId, status, null,startTime, endTime);
    }


    @Override
    public void updateDeliveryStatus(SysUser deliveryMan, String courierId, DeliveryStatusEnum status) {
        Courier courier = courierService.getByCourierId(courierId);

        if (courier.getPrice() == null) {
            throw new ServiceException("请完善顾客快递重量信息");
        }

        CourierDelivery courierDelivery = courierDeliveryService.getByCourierIdAndDeliveryManId(courier.getCourierId(),
                deliveryMan.getId());
        if (status != DeliveryStatusEnum.FAIL && status.getValue() < courierDelivery.getStatus().getValue()) {
            throw new ServiceException("不能逆向修改状态");
        }
        if (status == courierDelivery.getStatus()) {
            return;
        }
        CourierStatusEnum courierStatus = null;
        Logistics logistics = null;
        //目的网点为空，表示是派送到顾客手上这一步骤
        if (courierDelivery.getDestNetWork() == null) {
            if (status == DeliveryStatusEnum.DELIVERED) {
                throw new ServiceException("不能代顾客签收，请扫描顾客签收二维码进行签收");
            }
            if (status == DeliveryStatusEnum.DELIVERING) {
                //派送中
                courierStatus = CourierStatusEnum.DELIVERY;
            }
        } else {
            //已送达
            if (status == DeliveryStatusEnum.DELIVERED) {
                if (Objects.equals(courier.getSender().getNetwork().getId(),
                        courierDelivery.getDestNetWork().getId())) {
                    //如果快递员派送的网点是寄件网点，则快递状态改为已揽件
                    courierStatus = CourierStatusEnum.COLLECTED;
                }
                //快递入库到达的网点
                networkAdminService.addCourierToNetwork(courierDelivery.getDestNetWork().getId(), courierId);
            }
            //运输中
            else if (status == DeliveryStatusEnum.DELIVERING) {
                if (!Objects.equals(courier.getSender().getNetwork().getId(),
                        courierDelivery.getDestNetWork().getId())) {
                    //如果快递员派送的网点不是寄件网点，则快递状态改为运输中
                    courierStatus = CourierStatusEnum.TRANSIT;
                    //快递从快递员的网点出库
                    networkAdminService.delCourierFromNetwork(deliveryMan.getNetwork().getId(), courierId);
                }
            }
        }

        if (status == DeliveryStatusEnum.DELIVERED) {
            logistics = new Logistics();
            logistics.setNetwork(courierDelivery.getDestNetWork());
            logistics.setCreateTime(new Date());
            logistics.setParent(courier.getLogistics());
            logisticsService.insert(logistics);
        }

        if (courier.getStatus() == courierStatus) {
            //如果状态没变，则不必更改状态
            courierStatus = null;
        }

        if (courierStatus != null || logistics != null) {
            courierService.update(courierId, null, courierStatus, logistics == null ? null : logistics.getId(), null,
                    courierStatus != null);
        }

        courierDeliveryService.updateStatus(deliveryMan.getId(), courierId, status,
                status == DeliveryStatusEnum.DELIVERED ? DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")
                        : null);

    }


    @Override
    public void updateCourier(SysUser user, String courierId, Double weight) {
        Courier courier = courierService.getByCourierId(courierId);
        if (!Objects.equals(courier.getSender().getNetwork().getId(), user.getNetwork().getId())) {
            throw new ServiceException("仅揽件的网点人员才可修改该快递信息");
        }
        CourierDelivery courierDelivery = courierDeliveryService.getByCourierIdAndDeliveryManId(courierId,
                user.getId());
        if (courierDelivery.getStatus() != DeliveryStatusEnum.WAIT) {
            throw new ServiceException("快递已派送，请务修改快递信息");
        }
        courierService.update(courierId, weight, null, null, null, false);
    }
}
